package com.sky.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.sky.dto.TimeoutDTO;
import com.sky.po.OrdersPO;
import com.sky.repository.OrderRepository;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Content: 定时任务类
 * @Author: Asen
 * @Date: 2023/9/14 20:10
 */
@Component
@Slf4j
public class OrderTask {
    @Autowired
    private OrderRepository orderRepository;
    /**
     * 处理超时订单
     * 每分钟触发一次
     */
    @Scheduled(cron = "0 * * * * ? ")
    public void processTimeoutOrder(){
//        log.info("开始处理超时订单");
        DateTime offset = DateUtil.offset(new Date(), DateField.MINUTE, -15);//获取当前时间15分钟前
        orderRepository.updateByStatus(new TimeoutDTO(offset, OrdersPO.PENDING_PAYMENT,OrdersPO.CANCELLED,"订单超时,自动取消"));
    }

    /**
     * 处理一直处于派送中状态的订单
     */
    @Scheduled(cron = "0 0 1 * * ?")//每天凌晨一点触发
    public void processDeliveryOrder(){
//        log.info("开始处理处理派送的的订单");
        DateTime offset = DateUtil.offset(new Date(),DateField.MINUTE,-60);//当前时间一小时前
        orderRepository.updateByStatus(new TimeoutDTO(offset,OrdersPO.DELIVERY_IN_PROGRESS,OrdersPO.COMPLETED,null));
    }
}
